package ldh.im.fxbase.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.media.*;
import javafx.stage.Stage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class VideoPlayerController implements Initializable {

    @FXML private MediaView mediaView;
    @FXML private ScrollPane scrollPane;
    @FXML private Slider timeSlider;

    private Stage stage;

    public void play(String audioPath) {
        File file = new File(audioPath);
        Media media = new Media(file.toURI().toString());
        MediaPlayer mediaPlayer = new MediaPlayer(media);
        mediaPlayer.currentTimeProperty().addListener((observable, oldValue, newValue) -> {
            timeSlider.setValue(newValue.toSeconds());
        });
        mediaView.setMediaPlayer(mediaPlayer);

        mediaPlayer.setOnReady(() -> {
            double second = mediaPlayer.getTotalDuration().toSeconds();
            timeSlider.setMax(second);
            int unit = (int)(second/10);
            if (unit < 1) unit = 1;
            timeSlider.setMajorTickUnit(unit);

            int width = mediaPlayer.getMedia().getWidth();
            int height = mediaPlayer.getMedia().getHeight();
            if (width > 0 && height > 0) {
                stage.setWidth(width + 30);
                stage.setHeight(height + 150);
                stage.centerOnScreen();
                timeSlider.setPrefWidth(width);
            }
        });
        mediaPlayer.play();

//        ThreadPoolUtil.submit(()->{
//            double[] size = getMediaSize(audioPath);
//            FxThreadUtil.runLater(()->{
//                stage.setWidth(size[0] + 30);
//                stage.setHeight(size[1] + 150);
//                timeSlider.setPrefWidth(size[0]);
//            });
//        });
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public void dispose() {
//        MediaPlayer mediaPlayer = mediaView.getMediaPlayer();
//        if (mediaPlayer != null) {
//            mediaPlayer.dispose();
//            mediaView.setMediaPlayer(null);
//        }
        stage = null;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
//        timeSlider.valueProperty().addListener((observable, oldValue, newValue) -> {
//            if (mediaPlayer != null && mediaPlayer.getStatus() != MediaPlayer.Status.PAUSED && mediaPlayer.getStatus() != MediaPlayer.Status.HALTED
//                    && mediaPlayer.getStatus() != MediaPlayer.Status.STOPPED) {
//                mediaPlayer.seek(Duration.seconds(newValue.doubleValue()));
//            }
//        });

        timeSlider.setMin(0);
        timeSlider.setShowTickMarks(true);
        timeSlider.setShowTickLabels(true);
//        timeSlider.setMajorTickUnit(10); // 设置主刻度间隔，根据需要调整
        timeSlider.setMinorTickCount(0); // 设置次刻度数量，根据需要调整
        timeSlider.setBlockIncrement(10); // 设置块增量，根据需要调整

        mediaView.setOnError(e->{
            e.getMediaError().printStackTrace();
        });
    }

    private double[] getMediaSize(String videoFilePath) {
        try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFilePath)) {
            grabber.start();
            Frame frame = grabber.grabImage();
            if (frame != null) {
                Java2DFrameConverter converter = new Java2DFrameConverter();
                BufferedImage bufferedImage = converter.getBufferedImage(frame);
                return new double[] {bufferedImage.getWidth(), bufferedImage.getHeight()};
            }
            grabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
